package main

import (
	"fmt"
	"net"
	"os"
)

func CheckError(err error) {
	if err != nil {
		fmt.Println("Error: ", err)
		os.Exit(0)
	}
}

func main() {
	ServerAddr, err := net.ResolveUDPAddr("udp4", ":10001")
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp4", "10.0.8.17:10001")
	CheckError(err)

	RemoteAddr, err := net.ResolveUDPAddr("udp4", "10.0.8.17:30000")
	CheckError(err)

	Conn, err := net.DialUDP("udp4", LocalAddr, RemoteAddr)
	CheckError(err)

	defer func(Conn *net.UDPConn) {
		_ = Conn.Close()
	}(Conn)

	ServerConn, err := net.ListenUDP("udp4", ServerAddr)
	CheckError(err)

	defer func(ServerConn *net.UDPConn) {
		_ = ServerConn.Close()
	}(ServerConn)

	buf := make([]byte, 2048)

	for {
		n, addr, err := ServerConn.ReadFromUDP(buf)

		fmt.Println("Received ", n, "bytes from ", addr)

		if err != nil {
			fmt.Println("Error-recive: ", err)
		} else {
			go func() {
				_, _ = Conn.Write(buf[0:n])
			}()
		}

	}

}
